package ee.maoinvest.frontend.controller.command.electricitymetercommands;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import ee.maoinvest.ErrorMessage;
import ee.maoinvest.backend.model.data.views.OuterTerritoryElectricityMeter;
import ee.maoinvest.backend.services.ElectricityMeterService;
import ee.maoinvest.backend.services.implementations.ElectricityMeterServiceImpl;
import ee.maoinvest.frontend.controller.command.Command;
import ee.maoinvest.frontend.controller.formstringdataconverters.electricitymeterforms.ReplaceOrDeleteOuterTerritoryElectricityMeterFormConverter;
import ee.maoinvest.frontend.controller.requestprocessors.ElectricityMeterFormsRequestProcessor;

public class ReplaceOrDeleteOuterTerritoryElectricityMeterCommand implements Command
{
	private Logger logger = Logger.getLogger(ReplaceOrDeleteOuterTerritoryElectricityMeterCommand.class);
	private int operationResult = 0;
	private final int OPERATION_SUCCESSFUL = 1;
	private final int OPERATION_UNSUCCESSFUL = 0;
	private final int PROBABLY_A_DATABASE_ERROR = -1;
	
	@Override
	public int executeCommand(HttpServletRequest request,
			HttpServletResponse response) 
	{
		try 
		{
			ElectricityMeterFormsRequestProcessor electricityMeterFormsRequestProcessor = null;
			ReplaceOrDeleteOuterTerritoryElectricityMeterFormConverter replaceOrDeleteOuterTerritoryElectricityMeterFormConverter = null;
			OuterTerritoryElectricityMeter outerTerritoryElectricityMeter = null;
			ElectricityMeterService electricityMeterService = null;
			
			electricityMeterFormsRequestProcessor = new ElectricityMeterFormsRequestProcessor();
			replaceOrDeleteOuterTerritoryElectricityMeterFormConverter = 
					electricityMeterFormsRequestProcessor.fillReplaceOrDeleteOuterTerritoryElectricityMeterFormConverterWithRequestData(request);
			outerTerritoryElectricityMeter = replaceOrDeleteOuterTerritoryElectricityMeterFormConverter.convertToModelData();
			boolean dataIsOKAfterFormatting = replaceOrDeleteOuterTerritoryElectricityMeterFormConverter.getIsDataAfterFormattingOK();
			
			if (dataIsOKAfterFormatting)
			{
				electricityMeterService = new ElectricityMeterServiceImpl();
				int wasInsertionToDatabaseSuccessful = electricityMeterService.replaceOrDeleteOuterTerritoryElectricityMeter(
						outerTerritoryElectricityMeter);
				if (wasInsertionToDatabaseSuccessful == OPERATION_SUCCESSFUL)
				{
					operationResult = OPERATION_SUCCESSFUL;
				}
				else
				{
					request.setAttribute("replacingOrDeletingOuterTerritoryElectricityMeterFromADatabaseWasUnsuccessful", 
							ErrorMessage.E0008_ERROR_WHILE_TRYING_TO_DELETE_OUTER_TERRITORY_ELECTRICITY_METER_FROM_DATABASE);
					operationResult = OPERATION_UNSUCCESSFUL;
				}
			}
			else
			{
				operationResult = OPERATION_UNSUCCESSFUL;
			}

			request.setAttribute("replaceOrDeleteOuterTerritoryElectricityMeterFormConverter", 
					replaceOrDeleteOuterTerritoryElectricityMeterFormConverter);	
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
			return PROBABLY_A_DATABASE_ERROR;
		}
		
		return operationResult;
	}
}

